//
//  XQLaunchController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/1/27.
//

import UIKit

class XQLaunchController: XQBaseController {
    
    private lazy var bgImageView : UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "launch_bg")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var tryBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Try again", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        btn.backgroundColor = UIColor(hex: "9521F1")
        btn.layer.cornerRadius = 30
        btn.addTarget(self, action: #selector(tryClick), for: .touchUpInside)
        return btn
    }()
    
    var launchOptions : [UIApplication.LaunchOptionsKey: Any]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
        makeConstraints()
        
        startNetMonitoring();
        refreshData()
        
        XQHud.showLoading(controller: self)
    }

    
    private func makeUI() {
        self.view.addSubview(bgImageView)
        bgImageView.addSubview(tryBtn)
    }
    
    private func makeConstraints() {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        tryBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-UIDevice.xq_bottomSafeMargin()-54)
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.height.equalTo(54)
        }
    }
    
    private func refreshData() {
        let parameters: [String: Any] = [
            "credentials": "en",
            "question": 0,
            "technician": 0
        ]
             
        XQNetworkManager.shared.postRequest(url: "/figures/credentials", parameters: parameters) {[weak self] response, error in
                
            guard let srongify = self else {return}
            XQHud.hideLoading(controller: srongify)
            
            if error.isEmpty && response?.object == 0 {
               
                if let initModel = XQInitModel.deserialize(from: response?.procedure) {
                    
                    XQInitManager.shared.model = initModel
                    
                    XQNetworkManager.shared.setCountry(country: initModel.simple)
                    
                    if let fbModel = initModel.harassment {
                        XQAnalysisManager.shared.notifyFB(model: fbModel, launchOptions: srongify.launchOptions)
                    }
                    srongify.jumpToController()
                    srongify.notifyData()    
                    
                }
            }else {
                XQHud.showToast(message: "Network request error")
            }
        }
    }
    
    private func notifyData() {
        XQCityManager.shared.refreshData()
        XQAnalysisManager.shared.notifyDevice()
        XQLocationManager.shared.startLocation()
    }
    
    private func startNetMonitoring() {
        XQNetworkMonitor.shared.stopMonitoring()
        XQNetworkMonitor.shared.startMonitoring { flag in
            if (flag && XQInitManager.shared.model == nil) {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                    XQHud.showLoading(controller: self)
                    self.refreshData()
                }
            }
        }
    }
    
    private func jumpToController() {
        let showGuide = XQCache.getShowGuide()
       
        if (showGuide == false) {
            let welVC = XQWelcomeController()
            welVC.launchOptions = launchOptions
            self.navigationController?.setViewControllers([welVC], animated: false)
        }else {
            let mainVC = XQMainTabBarController()
            self.navigationController?.setViewControllers([mainVC], animated: false)
        }
    }
    
    @objc private func tryClick() {
        refreshData()
        
        XQHud.showLoading(controller: self)
    }
    
}
